package code.classic150on2025;

import java.util.*;

/**
 * @author 26029
 * @date 2025/6/19
 * @description
 */
public class DoublePointer {
    // 125. 验证回文串
    public boolean isPalindrome(String s) {
        char[] chars = s.toLowerCase().toCharArray();
        int left = 0, right = chars.length - 1;
        while (left < right) {
            if (!Character.isLowerCase(chars[left]) && !Character.isDigit(chars[left])) {
                left++;
                continue;
            }
            if (!Character.isLowerCase(chars[right]) && !Character.isDigit(chars[right])) {
                right--;
                continue;
            }
            if (chars[left] == chars[right]) {
                left++;
                right--;
            } else {
                return false;
            }
        }
        return true;
    }

    // 392. 判断子序列
    public boolean isSubsequence(String s, String t) {
        int sp = 0;
        int len = t.length();
        for (int i = 0; i < len; i++) {
            if (sp == s.length())
                return true;
            if (s.charAt(sp) == t.charAt(i))
                sp++;
        }
        return sp == s.length();
    }

    // 167. 两数之和 II - 输入有序数组
    public int[] twoSum(int[] numbers, int target) {
        int left = 0, right = numbers.length - 1;
        while (left < right) {
            if (left > 0 && numbers[left] == numbers[left - 1]) {
                left++;
                continue;
            }
            if (right < numbers.length - 1 && numbers[right] == numbers[right + 1]) {
                right--;
                continue;
            }
            if (numbers[left] + numbers[right] == target) {
                return new int[]{left + 1, right + 1};
            } else if (numbers[left] + numbers[right] > target) {
                right--;
            } else {
                left++;
            }
        }
        return null;
    }

    // 11. 盛最多水的容器
    public int maxArea(int[] height) {
        int left = 0, right = height.length - 1;
        int max = 0;
        while (left < right) {
            max = Math.max(max, (right - left) * Math.min(height[left], height[right]));
            if (height[left] < height[right])
                left++;
            else
                right--;
        }
        return max;
    }

    // 15. 三数之和
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> lists = new ArrayList<>();
        int len = nums.length;
        Arrays.sort(nums);
        for (int first = 0; first < len - 2; first++) {
            if (first != 0 && nums[first] == nums[first - 1])
                continue;
            int second = first + 1, third = len - 1;
            int target = -nums[first];
            while (second < third) {
                if (nums[second] + nums[third] == target) {
                    List<Integer> list = new ArrayList<>();
                    list.add(nums[first]);
                    list.add(nums[second]);
                    list.add(nums[third]);
                    lists.add(list);
                    second++;
                    third--;
                    while (second < third && nums[second] == nums[second - 1])
                        second++;
                    while (second < third && nums[third] == nums[third + 1])
                        third--;
                } else if (nums[second] + nums[third] < target) {
                    second++;
                } else {
                    third--;
                }
            }
        }

        return lists;
    }
}
